package com.imooc.bilibili.service;

import com.imooc.bilibili.dao.UserFollowingDao;
import com.imooc.bilibili.domain.FollowingGroup;
import com.imooc.bilibili.domain.User;
import com.imooc.bilibili.domain.UserFollowing;
import com.imooc.bilibili.domain.UserInfo;
import com.imooc.bilibili.domain.constant.UserConstant;
import com.imooc.bilibili.domain.exception.ConditionException;
import org.omg.CORBA.PRIVATE_MEMBER;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * ProjectName imooc-bilibili
 *
 * @author xieyucan
 * <br>CreateDate 2022/10/22 16:14
 */
@Service
public class UserFollowingService {

    @Resource
    private UserFollowingDao userFollowingDao;

    @Resource
    private FollowingGroupService followingGroupService;

    @Resource
    private UserService userService;

    //新增关注
    //获取分组id，判断分组是否为空，为空则存放在默认分组中,为关注设置分组
    //判断关注的用户是否存在
    //对用户进行关注，先删除在添加关注
    public void addUserFollow(UserFollowing userFollowing)
    {
        Long groupId = userFollowing.getGroupId();
        if(groupId==null) {
            FollowingGroup followingGroup =
                    followingGroupService.getByType(UserConstant.USER_FOLLOW_GROUP_TYPE_DEFAULT);
            userFollowing.setGroupId(followingGroup.getId());
        } else {
            FollowingGroup followingGroup = followingGroupService.getById(groupId);
            if(followingGroup==null){
                throw new ConditionException("关注的分组不存在");
            }
        }
        Long followingId = userFollowing.getFollowingId();
        User user=userService.getUserById(followingId);
        if(user==null){
            throw new ConditionException("关注的用户不存在");
        }
        userFollowingDao.deleteUserFollowing(userFollowing.getUserId(),followingId);
        userFollowing.setCreateTime(new Date());
        userFollowingDao.addUserFollowing(userFollowing);
    }

    //获取用户关注列表
    //根据用户id，查询用户基本信息
    //将关注用户进行分组
    public List<FollowingGroup> getUserFollowings(Long userId)
    {
        List<UserFollowing> list=userFollowingDao.getUserFollowings(userId);
        Set<Long> followIdSet=list.stream().map(UserFollowing::getFollowingId).collect(Collectors.toSet());
        List<UserInfo> userInfoList = new ArrayList<>();
        if(followIdSet.size()>0)
        {
            userInfoList=userService.getUserInfoByUserIds(followIdSet);
        }
        for(UserFollowing userFollowing:list)
        {
            for(UserInfo userInfo:userInfoList)
            {
                if(userFollowing.getFollowingId().equals(userInfo.getUserId())){
                    userFollowing.setUserInfo(userInfo);
                }
            }
        }
        List<FollowingGroup> groupList=followingGroupService.getByUserId(userId);
        //添加全部分组
        FollowingGroup allGroup = new FollowingGroup();
        allGroup.setName(UserConstant.USER_FOLLOWING_GROUP_ALL_NAME);
        allGroup.setFollowingUserInfoList(userInfoList);
        List<FollowingGroup> result = new ArrayList<>();
        result.add(allGroup);
        for(FollowingGroup group:groupList)
        {
            ArrayList<UserInfo> infoList = new ArrayList<>();
            for(UserFollowing userFollowing:list)
            {
                if(group.getId().equals(userFollowing.getGroupId()))
                {
                    infoList.add(userFollowing.getUserInfo());
                }
            }
            group.setFollowingUserInfoList(infoList);
            result.add(group);
        }
        return result;
    }


    //获取用户粉丝列表
    //根据粉丝的id查询基本信息
    //查询当前用户是否关注该粉丝
    public List<UserFollowing> getUserFans(Long userId)
    {
        List<UserFollowing> fanList=userFollowingDao.getUserFans(userId);
        Set<Long> fanIds=fanList.stream().map(UserFollowing::getUserId).collect(Collectors.toSet());
        List<UserInfo> userInfos = new ArrayList<>();
        if(fanIds.size()>0)
        {
            userInfos=userService.getUserInfoByUserIds(fanIds);
        }
        List<UserFollowing> followingList = userFollowingDao.getUserFollowings(userId);
        for(UserFollowing fan:fanList)
        {
            for(UserInfo userInfo:userInfos)
            {
                if(fan.getUserId().equals(userInfo.getUserId()))
                {
                    fan.setUserInfo(userInfo);
                }
            }
            for(UserFollowing userFollowing:followingList)
            {
                if(fan.getUserId().equals(userFollowing.getFollowingId())){
                    fan.getUserInfo().setFollowed(true);
                }
            }
        }
        return fanList;
    }

    // 添加用户分组
    public Long addUserFollowGroup(FollowingGroup followingGroup) {
        followingGroup.setCreateTime(new Date());
        followingGroup.setType(UserConstant.USER_FOLLOW_GROUP_TYPE_USER);
        followingGroupService.addUserFollowGroup(followingGroup);
        return followingGroup.getId();
    }

    //获取用户分组
    public List<FollowingGroup> getUserFollowingGroup(Long userId) {
        return followingGroupService.getUserFollowingGroup(userId);
    }


    public List<UserInfo> checkFollowStatus(List<UserInfo> list, Long userId) {

        List<UserFollowing> userFollowings = userFollowingDao.getUserFollowings(userId);
        for(UserInfo userInfo:list)
        {
            userInfo.setFollowed(false);
            for(UserFollowing userFollowing:userFollowings)
            {
                if(userInfo.getFollowed().equals(userFollowing.getFollowingId()))
                {
                    userInfo.setFollowed(true);
                }
            }
        }
        return list;
    }
}
